/**
 * 消息列表
 */
define(['../../common/service'],function(http){
    var NoticeList = {
        init: function () {
            var _this = this;

            _this.timeDelay			= 20000;				//前端显示数据延时时间[毫秒]
            _this.initTime 			= 50000;				//初始化界面时，渲染多少毫秒的数据[毫秒]
            //列表管理
            $('[list-manager="noticeList"]').listManager('init', {
                setTopVal:110//调整吸顶的高度,需要减去这个高度
                ,scrollDOM:$('.fmp-article')//监听滚动条
                ,isRealTime:true
                ,supportAjaxPage: true		//是否支持配置列表ajxa分页[多列表两种状态都有时，统一配置为true,禁用的列表pageJson参数为空即可]
                ,pageCallback:function(query){
                    _this.getList(query);
                }
            }, function(query){
                _this.getList(query);
            });
            _this.bindDelAction();
        }
        /*
         @初始化列表
         */
        ,getList: function(query){
            var _this = this;
            var loadingArea = $('.container-fluid');
            Kit.loading.show(loadingArea);
            http.work('/publishbatchs/searchAllPublishBatch',query,'POST').then(function(res){
                if(res.flag == 'success'){
                    var tmpHtml = '',
                        _list = res.data.publishBatchs,
                        _paginator = res.data.paginator,
                        _table = $('table[list-manager="ad-list"]'),
                        _tbody = $('.publish-batch-list', _table),
                        _serial = 0;		//序号
                    for(var i=0; i< _list.length; i++){
                        _serial = (_paginator.cPage-1) * _paginator.pSize +i + 1;
                        tmpHtml += _this.generateHTML(_list[i],_serial);
                    }
                    _tbody.html(tmpHtml);
                    _table.listManager([{'resetTd':false, 'resetPageData': _paginator}]);
                    //实时更新广告发布状态
                    _this.updataState();
                }else{
                    Kit.alert2(res.msg)
                }
                Kit.loading.hide(loadingArea);
            });
        }


        /*
         @获取列表数据
         */
        ,getList:function(query){
            var _this = this;
            var loadingArea = $('.container-fluid');
            Kit.loading.show(loadingArea);
            http.work('/notice/getNotice',query,'POST').then(function(res){
                Kit.loading.hide(loadingArea);
                var data = res.data;
                if(res.flag == 'success'){
                    //操作DOM节点更换数据
                    var _table = $('table[list-manager="noticeList"]'),
                        _tbody = $('#conditions-grid', _table),
                        tmpHtml = '',
                        _list = data.notice;
                    var type;

                    for(var i=0; i< _list.length; i++){
                        //if(_list[i].eventType === 'TOKEN_INVALID')
                        //{
                        //    type = 'token无效';
                        //}
                        //else if(_list[i].eventType === 'AUTHORITY_ALTERATION') {
                        //    type = '权限变更';
                        //}
                        //else if(_list[i].eventType === 'AD_PUBLISH_FAILURE') {
                        //    type = '广告发布失败';
                        //}
                        //else if(_list[i].eventType === 'ANNOUNCEMENT') {
                        //    type = '通知';
                        //}
                        type = _list[i].title;
                        var sendTime = moment(_list[i].sendTime).format('YYYY-MM-DD HH:mm:ss');
                        tmpHtml += '<tr data-id="'+ (_list[i].id || '') +'"'
                            + ' data-body="'+ _list[i].body +'"'
                            + '>';
                        tmpHtml +='<td align="center" title="' + _list[i].id + '">'
                            + (i+1)
                            + '</td>';

                        if(_list[i].isRead === 0) {
                            tmpHtml +=  '<td align="center" style="font-weight:700;"  title="' + _list[i].eventType + '">'
                                + type
                                + '</td>'
                                + '<td align="left" style="font-weight:700;"   title="' + sendTime + '">'
                                + sendTime
                                + '</td>'
                                + '<td align="left" style="font-weight:700;"   title="' + _list[i].body + '">'
                                + _list[i].body
                                + '</td>';
                        }
                        else{
                            tmpHtml +=  '<td align="center"  title="' + _list[i].eventType + '">'
                                + type
                                + '</td>'
                                + '<td align="left"  title="' + sendTime + '">'
                                + sendTime
                                + '</td>'
                                + '<td align="left"  title="' + _list[i].body + '">'
                                + _list[i].body
                                + '</td>';
                        }
                        tmpHtml +=  '<td>'
                            + '&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<span class="delete-action" style="cursor: pointer">'
                            + '<a data-original-title="Cancel">'
                            + '<i class="glyphicon glyphicon-trash"></i>'
                            + '</a>'
                            + '</span>'
                            + '</td>'
                            + '</tr>';
                    }
                    _tbody.html(tmpHtml);
                    _table.listManager([{'resetTd':false, 'resetPageData': data.paginator}]);
                }else{
                    Kit.alert2(msg);
                }
            });
        }
        /*
         @绑定删除事件
         */
        ,bindDelAction: function(){
            var _this = this;
            var _table = $('table[list-manager="noticeList"]')
            //删除事件
            _table.off('click', '.delete-action');
            _table.on('click', '.delete-action', function(){
                var _deleteAction = $(this),
                    _tr = _deleteAction.parents('tr').eq(0),
                    _id = _tr.attr('data-id');
                _this.editTr = _tr;
                var par = {
                    id : _id || ''
                }

                if(par.id == ''){
                    return false;
                }
                if(_deleteAction.find('i').length > 1){  //防止多次点击
                    return false;
                }
                var _loadIcon = $('<i class="fa fa-refresh refresh-rotation" style="display:none;"></i>');
                _deleteAction.append(_loadIcon).css('marginRight', '-16px');
                _loadIcon.fadeIn(300);
                http.work('/notice/delNotice', par, 'POST').then(function(res){
                    var data = res.data;
                    if(res.flag == 'success'){
                        _loadIcon.fadeOut(300,function(){
                            _loadIcon.remove();
                            _deleteAction.css('marginRight', '0px');
                            $('#conditions-grid tr[data-id='+_id+']').remove();
                            //少了参数
                            //			tPage: 10,				//总页数
                            //			cPage: 1,				//当前页
                            //			pSize: 10,				//每页显示条数
                            //			tSize: 100
                            var query = {
                                cPage: $("ul[class='pagination'] li[class='active']").find("a").html()*1,
                                pSize: $('.pSizeArea').attr('value')
                            }
                            if($('#conditions-grid tr').length==0){//如果删除到最后一页的最后一条数据
                                query.cPage=query.cPage>1?query.cPage-1:query.cPage;
                            }
                            _this.getList(query);
                        });
                        //清除消息列表
                        if($("#" + par.id).length>0){
                            $("#" + par.id).remove();
                            var num = $("#message-num").html();
                            if(num > 0){
                                $("#message-num").html(num - 1);
                                $('#message-num').data("num", num - 1);
                            }
                        }
                        //reset 分页插件

                    }else{
                        Kit.alert2(msg);
                    }
                });
            });

        }
    }
    return NoticeList;
});
